package com.lanswon.qzsmk.controller.inventoryManageController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.HttpResponseException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.annotation.SystemControllerLog;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.InventoryException;
import com.lanswon.qzsmk.model.ActiveUser;
import com.lanswon.qzsmk.model.CooperationCompany;
import com.lanswon.qzsmk.service.systemManageService.InventoryManagementService;
import com.lanswon.qzsmk.vo.ResponseCardFileVo;

import ch.qos.logback.classic.Logger;


@Controller
@RequestMapping(value = "/inventoryIncmoingMagOfCenter")
public class InventoryIncomingMagOfCenterController {
	
	private final static Logger logger = (Logger) LoggerFactory.getLogger(InventoryIncomingMagOfCenterController.class);
	
	@Autowired
	InventoryManagementService ims;
	
	/**
	 * 子模块：采购卡入库 
	 * 功能：跳转采购卡入库页面
	 * @return
	 */
	@RequiresPermissions("inventoryIncmoingMagOfCenter:index")
	@RequestMapping(name = "制卡返回中心库入库,inventoryIncmoingMagOfCenter:index", value = "/queryCardPurchase", method = RequestMethod.GET)
	@SystemControllerLog(description = "点击进入采购卡入库页面")
	public String queryCardPurchase(HttpServletRequest request) {
		return "PurchaseCardQuery";
	}

	/**
	 * 子模块：采购卡入库 
	 * 功能：获取文件信息
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequiresPermissions("inventoryIncmoingMagOfCenter:getFilesInfo")
	@RequestMapping(name = "获取文件信息,inventoryIncmoingMagOfCenter:getFilesInfo", value = "/getFilesInfo", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "获取文件信息请求")
	public Map<String, Object> getFilesInfo(HttpServletRequest request,Integer pageNum,Integer numPerPage) throws IOException {
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		Map<String, Object> map = new HashMap<>();
		
		logger.debug("页面currentPage==>" + pageNum);
		logger.debug("页面numPerPage==>" + numPerPage);
		if (pageNum == null && numPerPage == null) {
			pageNum = 1;
			numPerPage = 10;
		}
		// 获取页面上的值
		String makeCardNo = request.getParameter("makeCardNo");
		String companies = request.getParameter("companies");
		logger.debug("页面的制卡批号==>" + makeCardNo);
		logger.debug("页面的制卡商==>" + companies);
		Map<String, String> tmap = new HashMap<String, String>();
		tmap.put("makeCardNo", makeCardNo);
		tmap.put("companies", companies);
		DwzPage<ResponseCardFileVo> dPage = ims.queryPurchaseCardFiles(tmap, pageNum, numPerPage);

		map.put("dPage", dPage);
		logger.debug("activeUser====>" + activeUser);
		map.put("activeUser", activeUser);

		return map;
	}

	/**
	 * 子模块：采购卡入库 
	 * 功能：查询功能，获取返回文件列表
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequiresPermissions("inventoryIncmoingMagOfCenter:getResponseFileNo")
	@RequestMapping(name = "查询获取返回文件列表,inventoryIncmoingMagOfCenter:getResponseFileNo", value = "/getResponseFileNo", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "查询文件信息")
	public Map<String, Object> getResponseFileNo(HttpServletRequest request, Integer currentPage, Integer numPerPage) throws IOException {
		if (currentPage == null && numPerPage == null) {
			currentPage = 1;
			numPerPage = 10;
		}
		String companies = "";
		String makeCardNo = request.getParameter("makeCardNo");
		String companie = request.getParameter("companies");
		logger.debug("页面的制卡批号==>" + makeCardNo);
		logger.debug("页面的制卡商==>" + companie);
		Map<String, String> map = new HashMap<String, String>();
		if (!"".equals(companie) && companie != null) {
			CooperationCompany comPanies = ims.CooperationCompanyMapper(companie);
			companies = comPanies.getCompanyName();
		}
		map.put("makeCardNo", makeCardNo);
		map.put("companies", companies);
		DwzPage<ResponseCardFileVo> dPage = ims.queryPurchaseCardFiles(map, currentPage, numPerPage);
		Map<String, Object> rMap = new HashMap<>();
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		rMap.put("dPage", dPage);
		rMap.put("activeUser", activeUser);
		return rMap;
	}

	/**
	 * 子模块：采购卡入库 
	 * 功能：转发入库登记页面
	 * @param request
	 * @return
	 */
	@RequiresPermissions("inventoryIncmoingMagOfCenter:toIncomingReg")
	@RequestMapping(name = "转发入库登记页面,inventoryIncmoingMagOfCenter:toIncomingReg", value = "/toIncomingReg", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "转发进入入库登记页面")
	public Map<String, Object> toIncomingReg(String[] responseFiletasked, HttpServletRequest request) {
		// 装载制卡文件号的list
		List<Map<String, String>> makeCardFileNoList = new ArrayList<Map<String, String>>();		
		makeCardFileNoList = ims.getMakeCardFileNoList(responseFiletasked);		

		request.getSession().removeAttribute("responseFileNoList");
		request.getSession().setAttribute("responseFileNoList", makeCardFileNoList);

		Map<String, Object> rmap = new HashMap<>();
		// 跳转页面传值使用的map
		rmap.put("statusCode", "200");
		rmap.put("callbackType", "forward");
		rmap.put("forwardUrl", "inventoryIncmoingMagOfCenter/toIncomingRegPage");
		return rmap;
	}

	/**
	 * 子模块：采购卡入库 功能：跳转登记画面
	 * 
	 * @param responseFiletasked
	 * @param request
	 * @return
	 */
	@RequiresPermissions("inventoryIncmoingMagOfCenter:toIncomingRegPage")
	@RequestMapping(name = "跳转入库登记画面,inventoryIncmoingMagOfCenter:toIncomingRegPage", value = "/toIncomingRegPage", method = RequestMethod.POST)
	@SystemControllerLog(description = "跳转入库登记画面")
	public String toIncomingRegPage(HttpServletRequest request) {
		return "PurchaseCardIncomingReg";
	}

	/**
	 * 子模块：采购卡入库 
	 * 功能：从入库查询画面传值信息，到登记画面
	 * @param request
	 * @return
	 */
	@RequiresPermissions("inventoryIncmoingMagOfCenter:getIncomingDetail")
	@RequestMapping(name = "入库查询信息,inventoryIncmoingMagOfCenter:getIncomingDetail", value = "/getIncomingDetail", method = RequestMethod.GET)
	@ResponseBody
	@SystemControllerLog(description = "入库查询信息")
	public Map<String, Object> getIncomingDetail(HttpServletRequest request) {
		// 返回的map对象
		Map<String, Object> rmap = new HashMap<>();

		Map<String, Object> map = new HashMap<>();
		// 获取所选中的文件信息列表
		logger.debug("responseFileNoList==>" + request.getSession().getAttribute("responseFileNoList").toString());
		map.put("fileInfoList", request.getSession().getAttribute("responseFileNoList"));
		request.getSession().removeAttribute("responseFileNoList");
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		List<Map<String, Object>> incomingDetail = ims.getIncomingDetail(map);
//			logger.debug("incomingDetail的大小==>" + incomingDetail);
		rmap.put("incomingDetail", incomingDetail);
		rmap.put("activeUser", activeUser);
		return rmap;
	}

	/**
	 * 子模块：采购卡入库 
	 * 功能：打开详细信息对话框
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequiresPermissions("inventoryIncmoingMagOfCenter:getResponseFileDetail")
	@RequestMapping(name = "详细信息对话框,inventoryIncmoingMagOfCenter:getResponseFileDetail", value = "/getResponseFileDetail", method = RequestMethod.GET)
	@SystemControllerLog(description = "详细信息对话框")
	public String getResponseFileDetail(HttpServletRequest request) throws Exception {
		String param = URLDecoder.decode(request.getParameter("param"), "utf-8");
		logger.debug("responseFileNo==>" + param);
		// 解析文件的各个参数
		Map<String, Object> detailMap = ims.queryResponseFileDetail(param);

		logger.debug("detailMap的列表==>" + detailMap.get("fileDetail"));
		logger.debug("detailMap的供应商==>" + detailMap.get("company"));
		request.getSession().removeAttribute("detailMap");
		request.getSession().setAttribute("detailMap", detailMap);
		return "ResponseDetailInfo";
	}

	/**
	 * 子模块：采购卡入库 
	 * 功能：获取返回文件列表
	 * @param request
	 * @return
	 */
	@RequiresPermissions("inventoryIncmoingMagOfCenter:loadDetailInfo")
	@RequestMapping(name = "获取返回文件列表,inventoryIncmoingMagOfCenter:loadDetailInfo", value = "/loadDetailInfo", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "获取返回文件列表")
	public Map<String, Object> loadDetailInfo(HttpServletRequest request) {
		Map<String, Object> rmap = new HashMap<>();
		rmap.put("detailMap", request.getSession().getAttribute("detailMap"));
		request.getSession().removeAttribute("detailMap");
		return rmap;
	}

	/**
	 * 子模块：采购卡入库登记 功能：入库登记操作
	 * 
	 * @param request
	 * @return
	 * @throws InventoryException 
	 * @throws ApplicationException 
	 * @throws HttpResponseException 
	 */
	@RequiresPermissions("inventoryIncmoingMagOfCenter:incomingRegister")
	@RequestMapping(name = "入库登记操作,inventoryIncmoingMagOfCenter:incomingRegister", value = "/incomingRegister", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "入库登记操作")
	public Map<String, Object> incomingRegister(HttpServletRequest request) throws InventoryException{
		Map<String, Object> map = new HashMap<String, Object>();
		// 装载制卡文件号的list
		String storageNo = request.getParameter("storageNo");
		logger.debug("入库单号==>" + storageNo);
		String cardPrimaryCode = request.getParameter("cardPrimaryCode");
		logger.debug("卡产品名称==>" + cardPrimaryCode);
		String serializeForm = request.getParameter("serializeForm");
		logger.debug("页面所有信息==>" + serializeForm);
		// 更新信息,并返回打印需要的参数rMap
		Map<String, Object> rMap = new HashMap<String, Object>();

		
		rMap = ims.updateInfo(serializeForm);
		Map<String,Object> pramMap = new HashMap<String,Object>();
		pramMap.put("applyType", rMap.get("credential_type"));
		map.put("ipAddress", rMap.get("ipAddress"));
		map.put("storageNo", rMap.get("storageNo"));
		map.put("max", "true");
		map.put("title", "入库清单");
		map.put("rel", "incomingRegPrint");
		map.put("statusCode", "200");
		map.put("param", pramMap);
		map.put("message", "入库成功");
		
		map.put("jumpToTabid", "purchaseCardIncome");
		map.put("jumpToUrl", "inventoryIncmoingMagOfCenter/queryCardPurchase");
		map.put("jumpToTile", "制卡返回中心库入库");
		map.put("url", "printCredentials/toStoragePdfView");// post请求

		return map;
	}
}
